package secondhand

import (
	"fmt"
	"gitee.com/theegg/go-brick/app/model/user/users"
	"gitee.com/theegg/go-brick/app/service/app"
	"github.com/gin-gonic/gin"
)

// 二手物品列表
func List(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")

	if !users.HasUserFromName(username) {
		app.NewResponse(c).JsonReturn(false, "登陆失败, 请检查用户名或者密码(0x001)!", nil)
		return
	}

	user, err := users.UserLogin(username, password, c.ClientIP())
	if err != nil {
		fmt.Println(err)
		app.NewResponse(c).JsonReturn(false, "登陆失败, 请检查用户名或者密码!", nil)
		return
	}

	// 生成token
	token, errToken := user.GenToken()
	if errToken != nil {
		app.NewResponse(c).JsonReturn(false, errToken.Error(), nil)
		return
	}

	data := make(map[string]interface{})
	data["token"] = token
	app.NewResponse(c).JsonReturn(true, "登陆失败, 请检查用户名或者密码!", data)
	return
}
